package com.bjnangle.nangletool;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YFB on 2017/8/17.
 */

public class VersionActivity extends BaseActivity{
    private List<PackageInfo> allPackageInfos; // 取得系统安装所有软件信息
    private TextView apkName,apkVersion,guardName,guardVersion;
    private Map<String,Integer> apkinfo = new HashMap<>();
    private TextView tvTitle;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setActivity(this);
        setContentView(R.layout.activity_version);
        apkName = (TextView)findViewById(R.id.apk_name);
        apkVersion = (TextView)findViewById(R.id.apk_version);
        guardName = (TextView)findViewById(R.id.apk_guard_name);
        guardVersion = (TextView)findViewById(R.id.apk_guard_version);
        ivGoBack = (ImageView)findViewById(R.id.btn_back);
        tvTitle = (TextView)findViewById(R.id.tv_title);
        tvTitle.setText("版本检测");
        new Thread(new SeachThread()).start();
    }
    private class SeachThread extends Thread {
        @Override
        public void run() {
            if(allPackageInfos!=null){
                allPackageInfos.clear();
            }
            if(apkinfo!=null){
                apkinfo.clear();
            }
            allPackageInfos = getPackageManager().getInstalledPackages(
                    PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); // 取得系统安装所有软件信息
            int count = allPackageInfos.size();
            for (int i = 0; i < count; i++) {// 循环取出所有软件信息
                PackageInfo temp = allPackageInfos.get(i);
                ApplicationInfo appInfo = temp.applicationInfo;// 得到每个软件信息
                if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
                        || (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                } else {
                    Log.e("VersionActivity",temp.packageName);
                    apkinfo.put(temp.packageName,temp.versionCode);
                }
            }
            handler.sendEmptyMessage(1);
        }
    };
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(apkinfo.get("cn.bjnangle.eaglet.app")!=null){
                apkName.setText("南格床头分机");
                apkVersion.setText(""+apkinfo.get("cn.bjnangle.eaglet.app"));
            }else if(apkinfo.get("cn.bjnangle.eaglet.roomclient")!=null){
                apkName.setText("南格门口分机");
                apkVersion.setText(""+apkinfo.get("cn.bjnangle.eaglet.roomclient"));
            }else if(apkinfo.get("cn.bjnangle.eaglet.adminclient")!=null){
                apkName.setText("南格护士分机");
                apkVersion.setText(""+apkinfo.get("cn.bjnangle.eaglet.adminclient"));
            }else{
                apkName.setText("未安装南格应用");
                apkVersion.setText("");
            }
            if(apkinfo.get("cn.bjnangle.eaglet.guard.app")!=null){
                guardName.setText("守护进程");
                guardVersion.setText(""+apkinfo.get("cn.bjnangle.eaglet.guard.app"));
            }else{
                guardName.setText("未安装守护进程");
                guardVersion.setText("");
            }
        }
    };
}
